package com.bjpowernode.seckillservice.service.impl;

import com.bjpowernode.constant.Constants;
import jakarta.annotation.Resource;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * 下单后发送延迟消息
 */
@Component
public class DelayMessageService {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 下单后发送延迟消息
     * @param orderId
     */
    public void sendDelayMessage(Integer orderId) {
        //设置消息属性
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setHeader("x-delay", Constants.DELAY_TIME);

        //构建消息对象
        Message message = new Message(String.valueOf(orderId).getBytes(), messageProperties);

        //发送延迟消息
        rabbitTemplate.convertAndSend(Constants.EXCHANGE_DELAY_NAME, Constants.ROUTING_KEY_DELAY_NAME, message);

    }
}
